package com.pengcheng.taskapplication.mvp.presenter;



import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BaseEntity;
import com.pengcheng.taskapplication.net.ApiStore;
import com.pengcheng.taskapplication.net.ApiStoreV2;
import com.pengcheng.taskapplication.net.BaseSubscriber;
import com.pengcheng.taskapplication.net.HttpClient;
import com.pengcheng.taskapplication.net.HttpClientV2;
import com.pengcheng.taskapplication.util.SPUtils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Function;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by kekex on 2019/2/28.
 * basepresenter
 */

public class BasePresenterV2<T> implements Presenter<T> {

    public T iView;
    protected ApiStoreV2 iHttpUrlV2 = HttpClientV2.getRetrofit(ConstantValue.BASEURLV2).create(ApiStoreV2.class);
    private CompositeSubscription compositeSubscription;

    public BasePresenterV2(T view) {
        attachView(view);
    }

    /**
     * 绑定接口
     */
    @Override
    public void attachView(T view) {
        this.iView = view;
    }

    @Override
    public void detachView() {
        this.iView = null;
        onUnsubscribe();
    }

    /**
     * rxjava取消注册，以避免内存泄漏
     */
    public void onUnsubscribe() {
        if (compositeSubscription != null && compositeSubscription.hasSubscriptions()) {
            compositeSubscription.unsubscribe();
        }
    }

    /**
     * 进行网络请求
     */
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        compositeSubscription.add(observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
    }


//    /**
//     * 进行网络请求
//     */
//    public void addSubscription(Observable observable, Subscriber subscriber) {
//        if (compositeSubscription == null) {
//            compositeSubscription = new CompositeSubscription();
//        }
//
//
//        BaseSubscriber baseSubscriber=(BaseSubscriber) subscriber;
//
//       Observable observable1= iHttpUrlV2.checkToken(SPUtils.getString(baseSubscriber.context,ConstantValue.SpType.newtoken),SPUtils.getString(baseSubscriber.context,ConstantValue.SpType.userid));
//
//        compositeSubscription.add(observable1.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseSubscriber<BaseEntity>(baseSubscriber.context) {
//            @Override
//            public void onNext(BaseEntity entity) {
//                if (entity.result) {
//
//                } else {
//
//                }
//            }
//
//            @Override
//            public void onFailed(String code, String msg) {
//
//            }
//        }));
//
//
//        compositeSubscription.add(observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber));
//
//
//
//    }


}
